package com.wn.project.mapper;

import cn.hutool.core.util.StrUtil;
import com.wn.entity.Procedure;
import com.wn.entity.Project;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: pms
 * @InterfaceName ProcedureMapper
 * @description:
 * @author: LRW
 * @create: 2022-10-25 14:56
 * @Version 1.0
 **/
public interface ProcedureMapper {

    @Insert("insert into pms_procedure(no,project_id,name,description,principal,start_time,end_time,duration,status,path) values(#{no},#{projectId},#{name},#{description},#{principal},#{startTime},#{endTime},#{duration},#{status},#{path})")
    void add(Procedure procedure);

    @Delete("delete from pms_procedure where id=#{id}")
    void deleteById(int id);

    @Select("select * from pms_procedure where project_id=#{projectId}")
    List<Procedure> selectByProjectId(int id);

    @Select("select * from pms_procedure where id=#{id}")
    Procedure selectById(int id);

    @UpdateProvider(type = SqlProvider.class, method = "updateProcedure")
    void update(Procedure procedure);
    public static class SqlProvider {
        public static String updateProcedure(Procedure procedure) {
            String sql = new SQL() {
                {
                    UPDATE("pms_procedure");
                    if (StrUtil.isNotBlank(procedure.getName())) {
                        SET("name=#{name}");
                    }
                    if (StrUtil.isNotBlank(procedure.getNo())) {
                        SET("no=#{no}");
                    }
                    if (StrUtil.isNotBlank(procedure.getDescription())) {
                        SET("description=#{description}");
                    }
                    if (StrUtil.isNotBlank(procedure.getPrincipal())) {
                        SET("principal=#{principal}");
                    }
                    if (StrUtil.isNotBlank(procedure.getStartTime())) {
                        SET("start_time=#{startTime}");
                    }
                    if (StrUtil.isNotBlank(procedure.getEndTime())) {
                        SET("end_time=#{endTime}");
                    }
                    if (StrUtil.isNotBlank(procedure.getDuration())) {
                        SET("duration=#{duration}");
                    }
                    if (StrUtil.isNotBlank(procedure.getStatus())) {
                        SET("status=#{status}");
                    }
                    if (StrUtil.isNotBlank(procedure.getPath())) {
                        SET("path=#{path}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }
    }

}
